package com.ease.framework.gef4p.commands {

   import com.ease.framework.gef.IEditor;
   import com.ease.framework.gef4p.editparts.NodeEditPart;
   import com.ease.framework.gef4p.editparts.PoolEditPart;
   import com.ease.framework.gef4p.model.Node;
   import com.ease.framework.gef4p.model.Pool;
   import com.ease.framework.workbench.Workbench;
   import com.ease.framework.workbench.spi.impls.Command;

   public class CreateNodeCommand extends Command {
      public var process:Pool;
      public var activity:Node;

      //缓存所对应的editpart对象，便于在redo操作过程中避免再次创建.
      //但并不缓存editpart之间的关系，所有的undo, redo只是重新确立相互的关系而已.
      public var activityEditPart:NodeEditPart;

      //本command不再只更新model，同时会调用响应的editpart去操作。
      //中间省略notification环节（主要为了节省代码）

      public function CreateNodeCommand(pro:Pool, act:Node) {
         super();
         process = pro;
         activity = act;
      }

      override public function execute():void {
         process.addNode(activity);
         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(process) as PoolEditPart;
         activityEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(activity) as NodeEditPart;
         if (activityEditPart == null) {
            activityEditPart = IEditor(Workbench.instance.currentEditor).getEditPartFactory().createEditPart(activity) as NodeEditPart;
         }
         else {
            activityEditPart.unremove();
         }
         processEditPart.addNodeEditPart(activityEditPart);
      }

      override public function undo():void {
         var processEditPart:PoolEditPart = IEditor(Workbench.instance.currentEditor).getEditDomain().getEditPartByModel(process) as PoolEditPart;
         process.removeNode(activity);
         processEditPart.removeNodeEditPart(activityEditPart);
         activityEditPart.remove();
      }
   }
}